package utils

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

type ParamsChart struct {
	StartDate string `json:"startDate" form:"startDate"`
	EndDate   string `json:"endDate" form:"endDate"`
	Username  string `json:"username" form:"username"`
}

type ResSuccess struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

type ResFail struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

type ResWithPage struct {
	Code    int      `json:"code"`
	Message string   `json:"message"`
	Data    DatePage `json:"data"`
}

type DatePage struct {
	List     interface{} `json:"list"`
	Total    int64       `json:"total"`
	Page     int         `json:"page"`
	PageSize int         `json:"pageSize"`
}

func SuccessPagination(ctx *gin.Context, code int, message string, data interface{}, total int64, page, pageSize int) {
	if code == 0 {
		code = http.StatusOK
	}
	if message == "" {
		message = "success"
	}
	datePage := DatePage{
		List:     data,
		Total:    total,
		Page:     page,
		PageSize: pageSize,
	}
	res := ResWithPage{
		Code:    code,
		Message: message,
		Data:    datePage,
	}
	ctx.JSON(http.StatusOK, res)
}

func Success(ctx *gin.Context, code int, message string, data interface{}) {
	if code == 0 {
		code = http.StatusOK
	}
	if message == "" {
		message = "success"
	}
	res := ResSuccess{
		Code:    code,
		Message: message,
		Data:    data,
	}
	ctx.JSON(http.StatusOK, res)
}

func Fail(ctx *gin.Context, code int, message string) {
	if code == 0 {
		code = 400
	}
	if message == "" {
		message = "fail"
	}
	res := ResFail{
		Code:    code,
		Message: message,
	}
	ctx.JSON(http.StatusOK, res)
}

type Params struct {
	P
	Pages
}

type Pages struct {
	Page     int `form:"page" json:"page"`
	PageSize int `form:"pageSize" json:"pageSize"`
}

type P struct {
	ID       uint   `form:"id" json:"id"`
	Keyword  string `form:"keyword" json:"keyword"`
	UserId   int64  `form:"userId" json:"userId"`
	Username string `form:"username" json:"username"`
	Name     string `form:"name" json:"name"`
	Month    string `form:"month" json:"month"`
}
